package com.skyline.myspider.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.skyline.myspider.core.LoggerUtil;

public class Configuration {

	private static Logger logger = null;

	private static Properties propers = null;

	private static List<String> seeds = null;

	private static List<String> keywords = null;
	
	private static List<String> storageContent = null;

	private static float relevanceThreshold = 0;

	public static void config(String configProperPath, String logProperPath) throws ConfigFailException {
		logger = LoggerUtil.getLogger(Configuration.class, logProperPath);
		propers = loadProperties(configProperPath);
		getSeeds();
		if (getKeywords() != null) {
			configRelevanceThreshold();
		}
		 getStorageContent();
	}

	private static void configRelevanceThreshold() throws ConfigFailException {
		String relevanceThresholdConfig = propers.getProperty("RELEVANCE_THRESHOLD");
		if (relevanceThresholdConfig == null) {
			throw new ConfigFailException("Configuration RELEVANCE_THRESHOLD is configed!");
		} else {
			relevanceThreshold =Float.valueOf(relevanceThresholdConfig);
		}
	}

	public static float getRelevanceThreshold() {
		return relevanceThreshold;
	}
	
	public static List<String> getStorageContent() throws ConfigFailException {
		if (storageContent == null) {
			String storageContentConfig = propers.getProperty("SEEDS");
			if (storageContentConfig == null) {
				throw new ConfigFailException("Configuration SEEDS is configed!");
			} else {
				String storageContentArray[] = storageContentConfig.split(",");
				List<String> storageContentList = new ArrayList<String>();
				for (int i = 0; i < storageContentArray.length; i++) {
					storageContentList.add(storageContentArray[i]);
				}
				storageContent = storageContentList;
			}
		}
		return storageContent;
	}

	public static List<String> getSeeds() throws ConfigFailException {
		if (seeds == null) {
			String seedsConfig = propers.getProperty("SEEDS");
			if (seedsConfig == null) {
				throw new ConfigFailException("Configuration SEEDS is configed!");
			} else {
				String seedsArray[] = seedsConfig.split(",");
				List<String> seedsList = new ArrayList<String>();
				for (int i = 0; i < seedsArray.length; i++) {
					seedsList.add(seedsArray[i]);
				}
				seeds = seedsList;
			}
		}
		return seeds;
	}

	public static List<String> getKeywords() throws ConfigFailException {
		if (keywords == null) {
			String keywordsConfig = propers.getProperty("KEYWORDS");
			if (keywordsConfig != null) {
				String keywordsArray[] = keywordsConfig.split(",");
				List<String> keywordsList = new ArrayList<String>();
				for (int i = 0; i < keywordsArray.length; i++) {
					keywordsList.add(keywordsArray[i].toLowerCase());
				}
				keywords = keywordsList;
			}
		}
		return keywords;
	}

	private static Properties loadProperties(String filePath) {
		Properties props = new Properties();
		URL configDir = Configuration.class.getResource(filePath);
		File file = new File(configDir.getPath().replace("%20", " "));
		InputStream in = null;
		try {
			in = new FileInputStream(file);
			props.load(in);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(String.format("couldn't load the config file:%s", file.getAbsolutePath()), e);
		} finally {
			try {
				in.close();
			} catch (Exception e1) {
				e1.printStackTrace();
				logger.error(String.format("couldn't close the config file:%s", file.getAbsolutePath()), e1);
			}
		}
		return props;
	}
}
